package com.smartHire.service.impl;

import com.smartHire.dao.GoodsDao;
import com.smartHire.dao.OrderDao;
import com.smartHire.dao.Order_goodsDao;
import com.smartHire.domain.Goods;
import com.smartHire.domain.Order;
import com.smartHire.domain.Order_goods;
import com.smartHire.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("orderService")
public class OrderServiceImpl implements OrderService {
    @Resource(name="orderDao")
    private OrderDao orderDao;
    @Resource(name="order_goodsDao")
    private Order_goodsDao order_goodsDao;
    @Resource(name = "goodsDao")
    private GoodsDao goodsDao;

    @Override
    public int insertOrder(Order order) {
        try {
            int nums = orderDao.insertOrder(order);
            return nums;
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return 0;
    }

    @Override
    public int queryOrderRows() {
        try {
            int nums = orderDao.queryOrderRows();
            return nums;

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateOrderOG_Id(Order order) {
        try {
            int nums = orderDao.updateOrderOG_Id(order);
            return  nums;

        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int updateOrderStatus(Order order) {
        try {
            int nums = orderDao.updateOrderStatus(order);
            return nums;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Order queryOrderByOrder_Id(Order order) {
        try {
            Order order1 = orderDao.queryOrderByOrder_Id(order);
            return order1;

        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List queryOrderByType(String type, String U_Id) {
        try {
            List<Order> orders = null;
            Order order = new Order();
            order.setU_Id(U_Id);
            if(type.equals("2"))
            {
                order.setStatus(0);
                orders = orderDao.queryOrderByType(order);
                order.setStatus(1);
                List<Order> orders1 =orderDao.queryOrderByType(order);
                orders.addAll(orders1);
                order.setStatus(2);
                orders.addAll(orderDao.queryOrderByType(order));

            }else
            {
                order.setStatus(Integer.parseInt(type));
                orders = orderDao.queryOrderByType(order);
            }

            List list = new ArrayList();

            Date date = new Date();
//            date.setTime(date.getTime()+8*(1000 * 60 * 60 * 24));

            for(Order order1: orders)
            {
                Order_goods order_goods = new Order_goods();
                order_goods.setOrder_Id(order1.getOrder_Id());
                order_goods = order_goodsDao.selectOrder_goodsByOrder_Id(order_goods);



                if(order1.getStatus()==1)
                {
                    Timestamp date2 = order1.getOrder_time();
                    long past = (date.getTime()-date2.getTime())/ (1000 * 60 * 60 * 24);
                    int day  = (int) (order1.getDay()-past);
                    int rest = (int) (order_goods.getRest()-past);

                    if(rest>order1.getDay())
                    {
                        System.out.println(1);
                        order_goods.setRest(rest);
                    }
                    else if(rest<=order1.getDay())
                    {
                        if(rest<=0)
                        {
                            order_goods.setRest(0);
                            order_goodsDao.updateOrder_goods(order_goods);

                            order1.setStatus(2);
                            orderDao.updateOrderStatus(order1);
                        }
                        else if(order_goods.getRest()>order1.getDay())
                        {
                            order_goods.setRest(rest);
                            order_goodsDao.updateOrder_goods(order_goods);
                        }
                        else if(day<=0)
                        {

                            order_goods.setRest(0);
                            order_goodsDao.updateOrder_goods(order_goods);

                            order1.setStatus(2);
                            orderDao.updateOrderStatus(order1);
                        }
                        else if(day<order_goods.getRest())
                        {
                            order_goods.setRest(day);
                            order_goodsDao.updateOrder_goods(order_goods);
                        }
                    }

                }




                list.add(order_goods);
                list.add(order1);

                Goods goods = new Goods();
                goods.setGood_id(order1.getGood_id());
                goods = goodsDao.selectGoodsByID(goods);

                list.add(goods);

            }
            return list;

        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List queryOrderByShop(String type, String shop_name) {
        try {
            List<Order> orders = null;
            Order order = new Order();
            order.setShop_name(shop_name);
            if(type.equals("2"))
            {
                order.setStatus(0);
                orders = orderDao.queryOrderByShop(order);
                order.setStatus(1);
                List<Order> orders1 =orderDao.queryOrderByShop(order);
                orders.addAll(orders1);
                order.setStatus(2);
                orders.addAll(orderDao.queryOrderByShop(order));
                order.setStatus(5);
                orders.addAll(orderDao.queryOrderByShop(order));

            }else
            {
                order.setStatus(Integer.parseInt(type));
                orders = orderDao.queryOrderByShop(order);
            }

            List list = new ArrayList();

            Date date = new Date();
//            date.setTime(date.getTime()+8*(1000 * 60 * 60 * 24));

            for(Order order1: orders)
            {
                Order_goods order_goods = new Order_goods();
                order_goods.setOrder_Id(order1.getOrder_Id());
                order_goods = order_goodsDao.selectOrder_goodsByOrder_Id(order_goods);



                if(order1.getStatus()==1)
                {
                    Timestamp date2 = order1.getOrder_time();
                    long past = (date.getTime()-date2.getTime())/ (1000 * 60 * 60 * 24);
                    int day  = (int) (order1.getDay()-past);
                    int rest = (int) (order_goods.getRest()-past);

                    if(rest>order1.getDay())
                    {
                        System.out.println(1);
                        order_goods.setRest(rest);
                    }
                    else if(rest<=order1.getDay())
                    {
                        if(rest<=0)
                        {
                            order_goods.setRest(0);
                            order_goodsDao.updateOrder_goods(order_goods);

                            order1.setStatus(2);
                            orderDao.updateOrderStatus(order1);
                        }
                        else if(order_goods.getRest()>order1.getDay())
                        {
                            order_goods.setRest(rest);
                            order_goodsDao.updateOrder_goods(order_goods);
                        }
                        else if(day<=0)
                        {

                            order_goods.setRest(0);
                            order_goodsDao.updateOrder_goods(order_goods);

                            order1.setStatus(2);
                            orderDao.updateOrderStatus(order1);
                        }
                        else if(day<order_goods.getRest())
                        {
                            order_goods.setRest(day);
                            order_goodsDao.updateOrder_goods(order_goods);
                        }
                    }

                }




                list.add(order_goods);
                list.add(order1);

                Goods goods = new Goods();
                goods.setGood_id(order1.getGood_id());
                goods = goodsDao.selectGoodsByID(goods);

                list.add(goods);

            }
            return list;

        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public List<Order> queryOrderByOG_Id(Order order) {
        try {
            List<Order> orders = orderDao.queryOrderByOG_Ig(order);
            return orders;
        }catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Order> queryOrdersByOrder_Id(Order order) {
        try {
            List<Order> orders = orderDao.queryOrdersByOrder_Id(order);
            return orders;
        }catch (Exception e)
        {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    public List<Order> queryAllOrder() {
        try {
            List<Order> orders = orderDao.queryAllOrder();
            return orders;
        }catch (Exception e)
        {
            e.printStackTrace();
        }

        return null;
    }
}
